<!-- 企业画像平台 -->
<template>
  <div>
    <Detail ref="detail" />
    <Set ref="set" @ok="search" />
    <ZgNewPage
      ref="page"
      :showInlineBtn="false"
      :uniqueName="uniqueName"
      :pageOpt="pageOpt"
      :dataBtns="dataBtns"
      :extraBtns="extraBtns"
      :headers="headers"
      :conditions="conditions"
      :adminWidth="164"
      :scrollX="1500"
      @search="onSearch"
      @filter="onFilter"
      @header="mergeHeader"
      @admin="toAdmin"
      @extra="toExtra"
    >
      <template slot="columns">
        <a-table-column
          v-for="(h, i) in headers"
          :key="`column${i}`"
          :width="h.width === 0 ? 'auto' : h.width"
          align="center"
          :title="h.label"
        >
          <template slot-scope="record">
            <template v-if="h.code === 'creditCode'">
              <span class="fw8">{{ record[h.code] }}</span>
            </template>
            <template v-else>
              {{ record[h.code] }}
            </template>
          </template>
        </a-table-column>
      </template>
    </ZgNewPage>
  </div>
</template>
<script>
import Detail from './Detail.vue'
import Set from './Set.vue'
export default {
  components: {
    Detail,
    Set
  },
  data() {
    return {
      uniqueName: 'test',
      pageOpt: {
        showPageTitle: false,
        showConditionBtn: true,
        showHeaderConfig: true,
        showAttachBtn: false
      },
      conditions: [
        {
          code: 'companyName',
          compType: '单行文本',
          label: '企业名称',
          defaultVal: undefined,
          extra: { placeholder: '请输入企业名称' }
        },
        {
          code: 'companyCode',
          compType: '单行文本',
          label: '企业代码',
          defaultVal: undefined,
          extra: { placeholder: '请输入企业代码' }
        },
        {
          code: 'creditCode',
          compType: '单行文本',
          label: '信用代码',
          defaultVal: undefined,
          extra: { placeholder: '请输入信用代码' }
        }
      ],
      headers: [
        { code: 'companyCode', label: '企业代码', isShow: true, sort: 0 },
        { code: 'companyName', label: '企业名称', isShow: true, sort: 0, width: 200 },
        { code: 'creditCode', label: '信用代码', isShow: true, sort: 0 },
        { code: '4', label: '基础信息', isShow: true, sort: 0 },
        { code: '5', label: '严重违法', isShow: true, sort: 0 },
        { code: '6', label: '法律诉讼', isShow: true, sort: 0 },
        { code: '7', label: '经营信息', isShow: true, sort: 0 },
        { code: '8', label: '经营风险', isShow: true, sort: 0 },
        { code: '9', label: '知识产权', isShow: true, sort: 0 },
        { code: '10', label: '交易结算', isShow: true, sort: 0 },
        { code: '11', label: '更新时间', isShow: true, sort: 0 }
      ],
      dataBtns: [
        {
          key: 'detail',
          name: '详情',
          type: 'link',
          isShow: () => {
            return true
          }
        },
        {
          key: 'update',
          name: '更新',
          type: 'link',
          isShow: () => {
            return true
          }
        }
      ],
      extraBtns: [{ key: 1, name: '平台配置', icon: 'cog', type: 'primary' }],
      // 网关前缀
      gateWayPrefix: '/zg-scf'
    }
  },
  methods: {
    search() {
      const pageRef = this.$refs.page
      if (pageRef) {
        pageRef.search()
      } else {
        setTimeout(() => {
          this.search()
        }, 50)
      }
    },
    onSearch(param) {
      const diyHeaders = localStorage.getItem(this.currentUser.username + '_diyHeader_' + this.uniqueName)
      if (diyHeaders) {
        this.mergeHeader(JSON.parse(diyHeaders))
      }
      this.$http.post(`${this.gateWayPrefix}/img/categoryScore/page`, { ...param }, (data) => {
        const { total, records } = data
        this.$refs.page.endSearch({
          total: Number(total),
          list: records
        })
      })
    },
    toAdmin(opt) {
      if (opt.key === 'detail') {
        this.$refs.detail.open(opt.obj)
      } else if (opt.key === 'update') {
        this.confirm(() => {}, '确定要更新该条数据吗？')
      }
    },
    toExtra(opt) {
      if (opt.key === 1) {
        this.$refs.set.open()
      }
    },
    onFilter(f, param) {},
    mergeHeader(newHeaders) {
      if (!newHeaders) {
        return
      }
      this.headers = newHeaders
        .filter((t) => {
          return t.isShow
        })
        .sort((a, b) => {
          return a.sort - b.sort
        })
    }
  }
}
</script>

<style lang="scss" scoped></style>
